﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Markup;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using XamlingCore.SL.Controls.ControlStyle.Interfaces;
using XamlingCore.SL.Controls.ContentTemplate.Interfaces;
using System.Collections.Generic;

namespace XamlingCore.SL.Controls.Content
{
    [ContentProperty("Pointers")]
    public class XContentControl : ContentControl
    {
        private List<IContentTemplatePointer> pointers = new List<IContentTemplatePointer>();

        public IStyleSelector StyleSelector
        {
            get { return (IStyleSelector)GetValue(StyleSelectorProperty); }
            set { SetValue(StyleSelectorProperty, value); }
        }

      

        protected override void OnContentChanged(object oldContent, object newContent)
        {
            base.OnContentChanged(oldContent, newContent);
            UpdateStyle();
        }

        protected void UpdateStyle()
        {
            if(StyleSelector!=null)
            {
                var style = StyleSelector.SelectStyle(Content);
                if (style != null)
                {
                    Style = style;
                }
            }

            if(pointers.Count > 0)
            {
                DataTemplate template;
                foreach(var item in pointers)
                {
                    if (item.IsValid(ContentTemplateSelectorBinding))
                    {
                        ContentTemplate = item.Template;
                        break;
                    }
                }
            }
        }



        public object ContentTemplateSelectorBinding
        {
            get { return (object)GetValue(ContentTemplateSelectorBindingProperty); }
            set { SetValue(ContentTemplateSelectorBindingProperty, value); }
        }

        // Using a DependencyProperty as the backing store for ContentTemplateSelectorBinding.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty ContentTemplateSelectorBindingProperty =
            DependencyProperty.Register("ContentTemplateSelectorBinding", typeof(object), typeof(XContentControl), new PropertyMetadata(null, ContentTemplateSelectorBindingChanged));

        private static void ContentTemplateSelectorBindingChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            var obj = d as XContentControl;
            if(obj!=null)
            {
                obj.UpdateStyle();
            }
        }


        // Using a DependencyProperty as the backing store for StyleSelector.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty StyleSelectorProperty =
            DependencyProperty.Register("StyleSelector", typeof(IStyleSelector), typeof(XContentControl), new PropertyMetadata(null, StyleChanged));

        private static void StyleChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            var obj = d as XContentControl;
            if (obj != null)
            {
                obj.UpdateStyle();
            }
        }

        public List<IContentTemplatePointer> Pointers
        {
            get { return pointers; }
            set { pointers = value; }
        }
    }
}
